package net.maku.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.maku.framework.common.page.PageResult;
import net.maku.framework.common.service.impl.BaseServiceImpl;
import net.maku.system.convert.SysDictDataConvert;
import net.maku.system.dao.ExpressWarehouseMapper;
import net.maku.system.dao.SysDictDataDao;
import net.maku.system.entity.ExpressWarehousePojo;
import net.maku.system.entity.SysDictDataEntity;
import net.maku.system.query.SysDictDataQuery;
import net.maku.system.query.SysDictTypeDataQuery;
import net.maku.system.service.SysDictDataService;
import net.maku.system.vo.SysDictDataVO;
import net.maku.system.vo.SysDictTypeDataVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据字典
 *
 * @author NULL
 */
@Service
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataDao, SysDictDataEntity> implements SysDictDataService {

    @Resource
    ExpressWarehouseMapper expressWarehouseMapper;

    @Override
    public PageResult<SysDictDataVO> page(SysDictDataQuery query) {
        IPage<SysDictDataEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(SysDictDataConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private QueryWrapper<SysDictDataEntity> getWrapper(SysDictDataQuery query) {
        QueryWrapper<SysDictDataEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type_id", query.getDictTypeId());
        wrapper.orderByAsc("sort");

        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictDataVO vo) {
        SysDictDataEntity entity = SysDictDataConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictDataVO vo) {
        /**
         * 不知道以下代码作用，为修复报错，恢复原来的代码
         */
//        SysDictDataEntity entity = SysDictDataConvert.INSTANCE.convert(vo);
//        SysDictDataEntity sysDictDataEntity = getBaseMapper().selectById(vo.getId());
//        LambdaQueryWrapper<ExpressWarehousePojo> objectQueryWrapper = new LambdaQueryWrapper<>();
//        objectQueryWrapper.eq(ExpressWarehousePojo::getPort, sysDictDataEntity.getDictLabel());
//        objectQueryWrapper.eq(ExpressWarehousePojo::getPortType, sysDictDataEntity.getDictValue());
//        List<ExpressWarehousePojo> expressWarehousePojos = expressWarehouseMapper.selectList(objectQueryWrapper);
//
//        if (expressWarehousePojos.size() > 1) {
//            throw new RuntimeException("字典重复");
//        }
//        expressWarehousePojos.get(0).setPort(vo.getDictLabel());
//        expressWarehousePojos.get(0).setPortType(Integer.valueOf(vo.getDictValue()));
//        updateById(entity);
//        expressWarehouseMapper.updateById(expressWarehousePojos.get(0));

        SysDictDataEntity entity = SysDictDataConvert.INSTANCE.convert(vo);

        updateById(entity);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public List<SysDictTypeDataVO> queryDictData(SysDictTypeDataQuery query) {
        return baseMapper.selectRelList(query);
    }

    @Override
    public List<SysDictTypeDataVO> selectRelList(SysDictTypeDataQuery query) {
        return baseMapper.selectRelList(query);
    }
}